package gokyesil.game.aygitciklar.controller;

import gokyesil.game.aygitciklar.model.EndBox;

import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.World;

public class AygitController implements InputProcessor
{

	private boolean right;
	private boolean tempRight;
	private boolean left;
	private boolean tempLeft;
	private Body aygitBody;
	private float forceX;
	private World world;
	private Vector2 impulse;
	private Body eBoxBody;
	private Endrance endrance;
	public boolean canBeFinished;
	private Body whiteBody;
	public boolean isFinished;
	public boolean reset;
	private GroundNormal groundNormal;
	private boolean isCountingDown;
	private int countingNumber;
	private static final float MAXSPEED = 10.0f;
	private static final float MINSPEED = 0.03f;

	public AygitController(Body _aygitBody, Body _eBoxBody, Body _whiteBody, World _world)
	{
		aygitBody = _aygitBody;
		eBoxBody = _eBoxBody;
		whiteBody = _whiteBody;
		groundNormal = new GroundNormal();
		world = _world;
		impulse = new Vector2(0.0f, 0.0f);
		endrance = new Endrance(aygitBody, eBoxBody);
		world.setContactFilter(endrance);
		canBeFinished = false;
		isFinished = false;
		reset = false;
		isCountingDown = false;
		countingNumber = 20;
	}
	public void update()
	{
		aygitBody.applyLinearImpulse(impulse, aygitBody.getPosition(), false);
		if(aygitBody.getLinearVelocity().x < MINSPEED && aygitBody.getLinearVelocity().x > -MINSPEED)
		{
			aygitBody.setLinearVelocity(0.0f, aygitBody.getLinearVelocity().y);
		}
		if(aygitBody.getLinearVelocity().y < MINSPEED && aygitBody.getLinearVelocity().y > -MINSPEED)
		{
			aygitBody.setLinearVelocity(aygitBody.getLinearVelocity().x, 0.0f);
		}
		if(impulse.x != 0.0f || impulse.y != 0.0f)
		{
			System.out.println("impulse has been reset");
			impulse.set(0.0f, 0.0f);
		}
		if(aygitBody.getLinearVelocity().x < MAXSPEED && aygitBody.getLinearVelocity().x > -MAXSPEED)
		{
			aygitBody.applyForceToCenter(forceX, 0, true);
		}
		if(whiteBody.getPosition().y < -1.0f)
		{
			canBeFinished = true;
			if(endrance.isFinished)
			{
				isCountingDown = true;
			}
		}
		if(isCountingDown)
		{
			countingNumber--;
		}
		if(countingNumber <0)
		{
			isFinished = true;
		}
	}
	
	public boolean keyDown(int keycode) {
		if (keycode == Keys.D)
		{
			forceX = 100.0f;
			tempRight = true;
		}
		if (keycode == Keys.A)
		{
			forceX = -100.0f;
			tempLeft = true;
		}
		if (keycode == Keys.W)
		{
			Vector2 from = new Vector2(aygitBody.getPosition());
			Vector2 to = new Vector2(aygitBody.getPosition().x, aygitBody.getPosition().y-1.0f);
			world.rayCast(groundNormal, from, to);
			if(groundNormal.isOnTheGround)
			{
				impulse.set(groundNormal.normal.mul(32.0f).add(0,16.0f));
				groundNormal.reset();
			}
		}
		if (keycode == Keys.R)
		{
			reset = true;
		}

		return false;
	}

	public boolean keyUp(int keycode) {
		if (keycode == Keys.D)
		{
			tempRight = false;
			if(tempLeft)
			{
				forceX = -100.0f;
			}
			else
			{
				forceX = 0.0f;
			}
		}
		if (keycode == Keys.A)
		{
			tempLeft = false;
			if(tempRight)
			{
				forceX = +100.0f;
			}
			else
			{
				forceX = 0.0f;
			}
		}
		return false;
	}

	@Override
	public boolean mouseMoved(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean scrolled(int arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDragged(int arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean keyTyped(char arg0) {
		// TODO Auto-generated method stub
		return false;
	}

}
